package com.ruoyi.system.outto.game.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.model.domain.AccountInfo;
import com.ruoyi.system.model.domain.AccountPersonalDetails;
import com.ruoyi.system.model.domain.InfoImg;
import com.ruoyi.system.model.domain.SalePartner;
import com.ruoyi.system.model.mapper.AccountInfoMapper;
import com.ruoyi.system.model.mapper.AccountPersonalDetailsMapper;
import com.ruoyi.system.model.mapper.InfoImgMapper;
import com.ruoyi.system.model.mapper.SuprAuthMapper;
import com.ruoyi.system.outto.core.LCConstant;
import com.ruoyi.system.outto.core.MapUntils;
import com.ruoyi.system.outto.core.OutUrlConstant;
import com.ruoyi.system.outto.core.RegionalCovert;
import com.ruoyi.system.outto.dto.KghImg;
import com.ruoyi.system.outto.dto.MQLinkMessage;
import com.ruoyi.system.outto.game.CommonApi;
import com.ruoyi.system.outto.game.LCAPI;
import com.ruoyi.system.outto.game.MENGQAPI;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author lz
 * @version 1.0
 * @date 2022/6/5 16:19
 */
@Slf4j
@Service
public class MENGQAPIImpl implements MENGQAPI {

    @Resource
    private InfoImgMapper infoImgMapper;
    @Resource
    private SuprAuthMapper suprAuthMapper;
    @Resource
    private CommonApi commonApi;
    @Value("${desc.title}")
    private String descTitle;
    private SalePartner salePartner;

    @Resource
    private AccountInfoMapper accountInfoMapper;

    @Override
    public AjaxResult addGame(AccountInfo accountInfo, Long userId,SalePartner salePartner) {
        this.salePartner = salePartner;
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> params = params(accountInfo, userId);
        String uuid = UUID.fastUUID().toString();
        String timestamp = String.valueOf(new Date().getTime()/1000);
        String md5 = DigestUtils.md5Hex("2023022452790594"+"f220a3b4619e39cbc40c5f1f1c7b6344"+timestamp+uuid).toUpperCase();
        log.info("萌奇添加接口请求参数={}", JSONObject.toJSONString(params));
        String response = HttpUtils.sendPost(OutUrlConstant.MQADD, params.toString(), (String) null);
        log.info("萌奇添加接口返回参数={}", response);
        JSONObject resultJson = JSONObject.parseObject(response);
        if (!"true".equals(resultJson.getString("success"))) {
            result.put("code", 500);
        } else {
            result.put("code", 200);
        }

        result.put("req", JSONObject.toJSONString(params));
        result.put("res", response);
        result.put("msg", resultJson.getString("data"));
        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult editGame(AccountInfo accountInfo, Long userId,SalePartner salePartner) {
        this.salePartner = salePartner;
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> params = params(accountInfo, userId);
        Map<String, Object> canshu = new HashMap<>();
        String uuid = UUID.fastUUID().toString();
        String timestamp = String.valueOf(new Date().getTime()/1000);
        String md5 = DigestUtils.md5Hex("2023022452790594"+"f220a3b4619e39cbc40c5f1f1c7b6344"+timestamp+uuid).toUpperCase();
        canshu.put("x-kgh-token","2023022452790594");
        canshu.put("x-kgh-nonce",uuid);
        canshu.put("x-kgh-signature",md5);
        canshu.put("x-kgh-timestamp",timestamp);
        log.info("萌奇编辑接口请求参数={}", JSONObject.toJSONString(params));
        String response = HttpUtils.sendPostAddHeader(OutUrlConstant.MQADD, JSONObject.toJSONString(params),canshu);
        log.info("萌奇编辑接口返回参数={}", response);
        JSONObject resultJson = JSONObject.parseObject(response);
        if (!"true".equals(resultJson.getString("success"))) {
            result.put("code", 500);
        } else {
            result.put("code", 200);
        }
        result.put("req", JSONObject.toJSONString(params));
        result.put("res", response);
        result.put("msg", resultJson.getString("data"));
        return AjaxResult.success(result);
    }


    private Map<String, Object> params(AccountInfo accountInfo, Long userId) {
        Map<String, Object> params = new HashMap<>();
        String uuid = UUID.fastUUID().toString();
        String timestamp = String.valueOf(new Date().getTime()/1000);
        params.put("proxyId","======");
        String sign = DigestUtils.md5Hex("2023022452790594"+"f220a3b4619e39cbc40c5f1f1c7b6344"+timestamp+uuid).toUpperCase();
        params.put("sign",sign);
        params.put("timestamp",timestamp);
        Map<String, Object> action = new HashMap<>();
        action.put("gameCode", accountInfo.getAccountId());
        action.put("title", accountInfo.getDes().substring(0,90));
        action.put("description", accountInfo.getDes());
        action.put("price",accountInfo.getPrice());
        action.put("goodsImg", accountInfo.getTitleImg());
        //图片数组
        InfoImg infoImg = new InfoImg();
        infoImg.setInfoId(accountInfo.getId());
        infoImg.setType(Constants.STR_1);
        List<InfoImg> infoImgs = infoImgMapper.selectInfoImgList(infoImg);
        String [] imgstrs = new String[]{};
        for(int i = 0;i<infoImgs.size();i++){
            imgstrs=new String[]{infoImgs.get(i).toString()};
        }
        action.put("carouselImages", imgstrs);
        action.put("detailsImages", imgstrs);
        action.put("accountSource", "ORIGINAL_USER");
        action.put("compensationService", "SUPPORT");
        MQLinkMessage mqLinkMessage = new MQLinkMessage();
        mqLinkMessage.setLinkPhone("13252363254");
        mqLinkMessage.setLinkQq("156888625");
        action.put("linkMessage", mqLinkMessage);
        action.put("accountMessage",descTitle+accountInfo.getId());
        action.put("receivablesAccountType", "222111333");
        action.put("outGoodsNo", accountInfo.getAccountId());
        params.put("action",action);
        return params;
    }


    @Override
    public AjaxResult delGame(Long id, Long userId,SalePartner sa) {
        this.salePartner = salePartner;
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        AccountInfo accountInfo = accountInfoMapper.selectAccountInfoById(id);
        params.put("goodsKeyNo",accountInfo.getAccountId());
        params.put("outGoodsNo","=================================================");
        Map<String, Object> canshu = new HashMap<>();
        String uuid = UUID.fastUUID().toString();
        String timestamp = String.valueOf(new Date().getTime()/1000);
        String md5 = DigestUtils.md5Hex("2023022452790594"+"f220a3b4619e39cbc40c5f1f1c7b6344"+timestamp+uuid).toUpperCase();
        canshu.put("x-kgh-token","2023022452790594");
        canshu.put("x-kgh-nonce",uuid);
        canshu.put("x-kgh-signature",md5);
        canshu.put("x-kgh-timestamp",timestamp);
        log.info("萌奇编辑接口请求参数={}", JSONObject.toJSONString(params));
        String response = HttpUtils.sendPostAddHeader(OutUrlConstant.MQDELETE, JSONObject.toJSONString(params),canshu);
        log.info("萌奇编辑接口返回参数={}", response);
        JSONObject resultJson = JSONObject.parseObject(response);
        if (!"true".equals(resultJson.getString("success"))) {
            result.put("code", 500);
        } else {
            result.put("code", 200);
        }
        result.put("req", JSONObject.toJSONString(params));
        result.put("res", response);
        result.put("msg", resultJson.getString("data"));
        return AjaxResult.success(result);
    }



/*    private String getRea(){
        Map<String, Object> params = new TreeMap<>();
        params.put("platform","xtdanbao");

        String cover = MapUntils.getCover(params);
        cover = cover + "key=wvvbt1rjwvms7o7e319jm7p28jhbqdc3";
        String sign = DigestUtils.md5Hex(cover).toUpperCase();
        params.put("sign", sign);
//        String response = HttpUtils.sendPost(OutUrlConstant.LCADD, JSONObject.toJSONString(params),"application/json");
        String response = HttpUtils.sendPost("https://www.51youxihao.com/index.php?g=api&m=product&a=list_games", JSONObject.toJSONString(params), "application/json");
        System.out.println(response);
        return response;
    }*/

    public static void main(String[] args) {
        Map<String, Object> params = new TreeMap<>();
//        String response = HttpUtils.sendPost(OutUrlConstant.LCADD, JSONObject.toJSONString(params),"application/json");
        String response = HttpUtils.sendPost("http://47.109.33.94:10021/open/out/saveOrUpdate", JSONObject.toJSONString(params), "application/json");
        System.out.println(response);
    }

}
